﻿using System;
using System.Linq;
using System.Windows;
using Microsoft.Win32;
using System.IO;

namespace BigMailer
{
	public class UiCore
	{
		public static void ShowFinalLink(string finalUrl)
		{
			Clipboard.SetData( DataFormats.Text, finalUrl );
			MessageBox.Show( "This url has been added in the clipboard, use CTRL-V to access.\n\n" + finalUrl, "Final Url",
			                 MessageBoxButton.OK, MessageBoxImage.Information );
		}

		public static string ServiceUrl
		{
			get
			{
				using ( RegistryKey key = Registry.CurrentUser.OpenSubKey( @"SOFTWARE\Kennedy\Mailer", false ) )
				{
					if ( key == null )
					{
						return null;
					}
					return (string)key.GetValue( "ServiceUrl" );
				}
			}
			set
			{
				using ( RegistryKey key = Registry.CurrentUser.CreateSubKey( @"SOFTWARE\Kennedy\Mailer" ) )
				{
					key.SetValue( "ServiceUrl", value );
				}
			}
		}

		public static string ServiceUsername
		{
			get
			{
				using ( RegistryKey key = Registry.CurrentUser.OpenSubKey( @"SOFTWARE\Kennedy\Mailer", false ) )
				{
					if ( key == null )
					{
						return "";
					}
					return (string)key.GetValue( "ServiceUsername" ) ?? "";
				}
			}
			set
			{
				using ( RegistryKey key = Registry.CurrentUser.CreateSubKey( @"SOFTWARE\Kennedy\Mailer" ) )
				{
					key.SetValue( "ServiceUsername", value.Trim() );
				}
			}
		}

		public static string ServicePassword
		{
			get
			{
				using ( RegistryKey key = Registry.CurrentUser.OpenSubKey( @"SOFTWARE\Kennedy\Mailer", false ) )
				{
					if ( key == null )
					{
						return "";
					}
					return (string)key.GetValue( "ServicePassword" ) ?? "";
				}
			}
			set
			{
				using ( RegistryKey key = Registry.CurrentUser.CreateSubKey( @"SOFTWARE\Kennedy\Mailer" ) )
				{
					key.SetValue( "ServicePassword", value.Trim() );
				}
			}
		}

		public static void SaveHostCode()
		{
			MessageBox.Show( "Please choose a location to save the sample website and libraries", "Big Mailer",
							 MessageBoxButton.OK, MessageBoxImage.Information );

			string file = "SampleSiteHostingService.rar";
			string folder = Path.GetDirectoryName( typeof( UiCore ).Assembly.Location );

			SaveFileDialog dialog = new SaveFileDialog();
			dialog.FileName = file;
			dialog.Filter = "Rar files|*.rar";
			dialog.CreatePrompt = false;

			if (dialog.ShowDialog() == true)
			{
				File.Copy( Path.Combine(folder, file ), dialog.FileName );
			}
		}
	}
}